package cn.secby.wms.util;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Random;

public class Application extends javafx.application.Application {
    private int counter = 0;
    @Override
    public void start(Stage primaryStage) throws Exception {
        String path = "D:/workspace/secby/secby/services/secby-wms/src/main/java/cn/secby/wms/util/data.txt";
        TabuSearch model = new TabuSearch(new ReadDataUtil().getInstance(path));
        Solution solution = model.search();
        //
        Instance instance = solution.getInstance();
        AnchorPane pane = new AnchorPane();
        pane = plot(pane,solution);
        Canvas canvas = new Canvas(instance.getL(),instance.getW());
        pane.getChildren().add(canvas);
        canvas.relocate(100,100);
        // 绘制最外层的矩形
        canvas = draw(canvas,0,0,instance.getL(),instance.getW());
        // 添加按钮
        Button nextButton = new Button("Next +1");
        Canvas finalCanvas = canvas;
        nextButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                try {
                    PlaceSquare placeSquare = solution.getPlaceSquareList().get(counter);
                    draw(finalCanvas,placeSquare.getX(),placeSquare.getY(),placeSquare.getL(),placeSquare.getW());
                    counter++;
                }catch (Exception e){
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("已经没有可以放置的矩形了！");
                    alert.showAndWait();
                }
            }
        });
        //
        pane.getChildren().add(nextButton);
        primaryStage.setTitle("二维下料可视化");
        primaryStage.setScene(new Scene(pane,1000,600, Color.AQUA));
        primaryStage.show();
    }

    private AnchorPane plot(AnchorPane pane, Solution solution) {
        System.out.println(solution);

        // 绘制里面的矩形
//        List<PlaceSquare> placeSquareList = solution.getPlaceSquareList();
//        System.out.println(placeSquareList.size());
//        for (PlaceSquare placeSquare : placeSquareList) {
//            canvas = draw(canvas,placeSquare.getX(),placeSquare.getY(),placeSquare.getL(),placeSquare.getW());
//        }
        return pane;
    }

    private Canvas draw(Canvas canvas,double x,double y,double l,double w){
        GraphicsContext gc = canvas.getGraphicsContext2D();
        // 边框
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(4);
        gc.strokeRect(x,y,l,w);
        // 填充
        gc.setFill(new Color(new Random().nextDouble(),new Random().nextDouble(),new Random().nextDouble(),new Random().nextDouble()));
        gc.fillRect(x,y,l,w);
        return canvas;
    }

    public static void main(String[] args) {
        launch(args);
    }

}